package com.qianyu.controller.comm;


import cn.dev33.satoken.stp.StpUtil;
import com.qianyu.entity.base.BaseIdDto;
import com.qianyu.entity.base.Result;
import com.qianyu.entity.bean.User;
import com.qianyu.entity.dto.UserDTO;
import com.qianyu.helper.LoginHelper;
import com.qianyu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "登录管理", description = "wangzhicheng")
@RestController
@RequestMapping("/api/base")
@RequiredArgsConstructor
public class LoginController {

    private final UserService userService;

    /**
     * 前台登录
     */
    @PostMapping("/loginFront")
    @ApiOperation(value = "前台登录")
    Result<User> loginInFront(@Validated @RequestBody UserDTO.UserLoginDTO dto) {
        return Result.success(userService.loginFront(dto));
    }

    /**
     * 前台登录
     */
    @PostMapping("/loginBack")
    @ApiOperation(value = "后台登录")
    Result<User> loginInBack(@Validated @RequestBody UserDTO.UserLoginDTO dto) {
        return Result.success(userService.loginBack(dto));
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    @ApiOperation(value = "登出")
    Result<String> loginIn(@Validated @RequestBody BaseIdDto dto) {
        StpUtil.logout(dto.getId());
        return Result.success();
    }

    /**
     * 根据用户id查询用户信息
     */
    @GetMapping("/getInfo")
    @ApiOperation(value = "查询用户信息")
    Result<User> getInfo() {
        return Result.success(userService.getById(LoginHelper.getUserId()));
    }
}
